#include "buzzer.h"
#include "timer.h"

uint8_t num = 0;
/**
 * @brief 蜂鸣器初始化
 * @param  : 
 * @param  :
 * @retval 
 */
void Buzzer_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
//	TIM_OCInitTypeDef TIM_OCInitStructure;    
//	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//打开定时器15的时钟
	
//	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
//	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//	TIM_TimeBaseStructure.TIM_Period = 1000-1;
//	TIM_TimeBaseStructure.TIM_Prescaler = 48-1;
//	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
//	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//	
//	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWMI1模式
//    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出比较状态使能
//    TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;//初始电平为高
//    TIM_OCInitStructure.TIM_Pulse = 1000-1;                //占空比的多少
//    TIM_OC2Init(TIM1,&TIM_OCInitStructure);  //不同的定时器通道是不一样的
//    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);//使能外设预装载寄存器在CCR4
//	
////	NVIC_InitStructure.NVIC_IRQChannel=TIM1_BRK_UP_TRG_COM_IRQn;
////	NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
////	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
////	NVIC_Init(&NVIC_InitStructure);


////    TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM3外设的主输出
//    //TIM_Cmd(TIM1,ENABLE);
//	BUZZER_OFF
	/**********初始化定时器3**********/
	Timer3_Init(0);
}

/**
 * @brief 	蜂鸣器响一段时间
 * @param  time: 响的时间单位为ms
 * @retval 
 */
void Buzzer_RunTime(uint16_t time)
{
	BUZZER_ON
	TIM_SetCounter(TIM3,0);
	TIM_SetAutoreload(TIM3,time);
	TIM_Cmd(TIM3,ENABLE);
}

/**
 * @brief 
 * @param  time: 时间
 * @param  次数:
 * @retval 
 */
void Buzzer_RunMul_Time()
{
	uint8_t i;
	for(i=0;i<2;i++)
	{
		BUZZER_ON
		delay_ms(50);
		BUZZER_OFF
		delay_ms(70);
	}
}


